<template>
  <div>
    <QSGSearchBar
      :formData="searchForm"
      :fields="listOption"
      @search="searchChange"
      @refresh="refreshChange"
      @reset="searchReset"
      ref="searchFormRef"
    >
      <!-- <template v-slot:appendTool>
        <el-button type="default" @click="configColumn"
          ><i class="el-icon-s-operation"></i
        ></el-button>
      </template> -->
    </QSGSearchBar>

    <avue-crud
      :option="listOption"
      :table-loading="loading"
      :data="listData"
      ref="listRef"
      v-model="listForm"
      :page.sync="page"
      :permission="permissionList"
      @search-change="searchChange"
      @search-reset="searchReset"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot-scope="{ row, index }" slot="menu">
        <el-button
          type="text"
          size="small"
          @click.stop="open(row, index)"
          v-if="row.applyStatus !== 1 && permission.product_openBtn"
          >{{ $t("productCenter.open") }}
        </el-button>
        <el-button
          type="text"
          size="small"
          @click.stop="renewal(row, index)"
          v-if="row.applyStatus === 1"
        >
          {{ $t("productCenter.renewal") }}
        </el-button>
      </template>
    </avue-crud>
    <QSGDrawer
      :title="drawerTitle"
      :fullscreen="false"
      size="30%"
      append-to-body
      :visible.sync="openDrawer"
      :wrapperClosable="false"
    >
      <el-form
        style="padding: 0px 20px"
        :label-position="labelPosition"
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
        size="small"
      >
        <el-form-item
          :label="$t('productCenter.expirationTime')"
          prop="expirationTime"
        >
          <el-date-picker
            style="width: 100%"
            v-model="ruleForm.expirationTime"
            type="datetime"
            :placeholder="$t('productCenter.selectTime')"
            :picker-options="pickerOptions"
            value-format="yyyy-MM-dd HH:mm:ss"
            format="yyyy-MM-dd HH:mm:ss"
            default-time="23:59:59"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>

      <template v-slot:footer>
        <el-button
          size="small"
          icon="el-icon-circle-plus-outline"
          type="primary"
          @click.stop="saveApply"
          >{{ $t("aicosCommon.saveBtn") }}</el-button
        >
        <el-button
          size="small"
          icon="el-icon-circle-close"
          @click.stop="closeApply"
          >{{ $t("kwAudit.close") }}</el-button
        >
      </template>
    </QSGDrawer>
  </div>
</template>
<script>
import { mapGetters } from "vuex";
import QSGSearchBar from "@/components/QSGSearchBar/index";
import QSGDrawer from "@/components/QSGDrawer/index";
import { getList, apply, renewal } from "@/api/operatingCenter/productCenter";
import serviceList from "@/config/serviceList";
import { getTextWidth } from "@/util/util";
import { baseURL } from "@/api/common";

const { naoai } = serviceList;
export default {
  name: "AicosFrontProductCenter",
  components: {
    QSGSearchBar,
    QSGDrawer,
  },
  data() {
    return {
      searchForm: {},
      loading: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 1,
        pageSizes: [10, 20, 30, 40, 50, 100],
      },
      listForm: {},
      selectionList: [],
      query: {},
      listData: [],
      listOption: {
        menuBtn: true,
        header: false,
        tip: false,
        searchShow: false,
        border: false,
        index: false,
        selection: false,
        viewBtn: false,
        delBtn: false,
        editBtn: false,
        columnBtn: true,
        menuWidth: 120,
        menuAlign: "center",
        dialogClickModal: false,
        dialogWidth: "30%",
        dialogType: "drawer",
        dialogCustomClass: "huilan-drawer-form",
        span: 24,
        labelWidth: 90,
        column: [
          {
            label: "First Name",
            prop: "",
          },
          {
            label: "Last Name",
            prop: "",
          },
          {
            label: this.$t("productCenter.phone"),
            prop: "account",
            overHidden: true,
            search: true,
            formatter: (val, value, label) => {
              if (this.validatenull(value)) {
                return "--";
              }
              return label;
            },
          },
          // {
          //   label: this.$t("productCenter.phone"),
          //   prop: "phone",
          //   overHidden: true,
          //   formatter: (val, value, label) => {
          //     if (this.validatenull(value)) {
          //       return "--";
          //     }
          //     return label;
          //   },
          //   search: true,
          // },
          // {
          //   label: this.$t("productCenter.clientType"),
          //   prop: "authType",
          //   type: "select",
          //   overHidden: true,
          //   dicData: [
          //     {
          //       label: this.$t("productCenter.enterprise"),
          //       value: 1,
          //     },
          //     {
          //       label: this.$t("productCenter.oneself"),
          //       value: 2,
          //     },
          //   ],
          //   formatter: (val, value, label) => {
          //     if (this.validatenull(value)) {
          //       return "--";
          //     }
          //     return label;
          //   },
          // },
          {
            label: this.$t("productCenter.companyName"),
            prop: "companyName",
            overHidden: true,
            formatter: (val, value, label) => {
              if (this.validatenull(value)) {
                return "--";
              }
              return label;
            },
            search: true,
          },
          {
            label: this.$t("productCenter.companySize"),
            prop: "companySize",
          },
          {
            label: this.$t("productCenter.product"),
            prop: "saasProductId",
            search: true,
            hide: true,
            searchType: "select",
            dicUrl: `${baseURL}api/${naoai}/saaSProduct/listAllSaaSProduct`,
            dicMethod: "post",
            overHidden: true,
            dicQuery: {
              languageContentType: 2,
            },
            props: {
              label: "name",
              value: "id",
            },
            dicFormatter: (res) => {
              let dicData = [];
              if (res.success) {
                res.data.map((item) => {
                  dicData.push({ name: item.name, id: item.id });
                });
              }
              return dicData;
            },
          },
          {
            label: this.$t("productCenter.product"),
            prop: "saasProductName",
            overHidden: true,
            formatter: (val, value, label) => {
              if (this.validatenull(value)) {
                return "--";
              }
              return label;
            },
          },
          // {
          //   label: "子产品",
          //   prop: "sonProductName",
          //   overHidden: true,
          //   // search:true,
          //   formatter: (val, value, label) => {
          //     if (this.validatenull(value)) {
          //       return "--";
          //     }
          //     return label;
          //   },
          // },
          {
            label: this.$t("productCenter.productType"),
            prop: "type",
            overHidden: true,
            // hide: true,
            // search: true,
            type: "select",
            // searchType: "select",
            dicData: [
              {
                label: this.$t("productCenter.parentProduct"),
                value: 0,
              },
              {
                label: this.$t("productCenter.sonProduct"),
                value: 1,
              },
            ],
          },
          {
            label: this.$t("productCenter.applyStatus"),
            prop: "applyStatus",
            type: "select",
            searchType: "select",
            dicData: [
              {
                label: this.$t("productCenter.notOpen"),
                value: 0,
              },
              {
                label: this.$t("productCenter.alreadyOpen"),
                value: 1,
              },
            ],
            search: true,
          },
          {
            label: this.$t("productCenter.applyStartTime"),
            prop: "applyStartTime",
            overHidden: true,
            formatter: (val, value, label) => {
              if (this.validatenull(value)) {
                return "--";
              }
              return label;
            },
          },
          {
            label: this.$t("productCenter.agreeTime"),
            prop: "agreeTime",
            overHidden: true,
            formatter: (val, value, label) => {
              if (this.validatenull(value)) {
                return "--";
              }
              return label;
            },
          },
          {
            label: this.$t("productCenter.expirationTime"),
            prop: "expirationTime",
            overHidden: true,
            formatter: (val, value, label) => {
              if (this.validatenull(value)) {
                return "--";
              }
              return label;
            },
          },
        ],
      },
      // 开通
      type: 0,
      drawerTitle: "",
      selectRow: {},
      openDrawer: false,
      ruleForm: {
        expirationTime: "",
      },
      rules: {
        expirationTime: [
          {
            required: true,
            message: this.$t("productCenter.selectTime"),
            trigger: "blur",
          },
        ],
      },
      labelPosition: "right",
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() < new Date().getTime();
        },
      },
    };
  },
  computed: {
    ...mapGetters(["permission", "language"]),
  },
  mounted() {},

  methods: {
    configColumn() {
      this.$refs.listRef.$refs.dialogColumn.columnBox = true;
    },
    open(row) {
      this.selectRow = row;
      this.openDrawer = true;
      this.drawerTitle = this.$t("productCenter.open");
      this.type = 1;
      this.ruleForm.expirationTime = "";
      this.labelPosition = this.language === "en" ? "top" : "right";
    },
    renewal(row) {
      this.selectRow = row;
      this.openDrawer = true;
      this.drawerTitle = this.$t("productCenter.renewal");
      this.type = 2;
      this.ruleForm.expirationTime = "";
      this.labelPosition = this.language === "en" ? "top" : "right";
    },
    saveApply() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          const item = {
            id: this.selectRow.id,
            applyStatus: 1,
            expirationTime: this.ruleForm.expirationTime,
          };
          if (this.type === 1) {
            apply(item).then((res) => {
              if (res.data.success) {
                this.openDrawer = false;
                this.onLoad(this.page);
              }
            });
          } else {
            renewal(item).then((res) => {
              if (res.data.success) {
                this.openDrawer = false;
                this.onLoad(this.page);
              }
            });
          }
        }
      });
    },
    closeApply() {
      this.openDrawer = false;
    },
    searchReset() {
      this.query = {};
      this.searchForm = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = {
        ...params,
      };
      this.searchForm = this.query;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },

    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },

    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      const dataParam = {
        ...params,
        ...this.query,
        current: page.currentPage,
        size: page.pageSize,
        languageContentType: 2,
      };
      getList(dataParam).then((res) => {
        const dataRes = res.data.data;
        this.page.total = dataRes.total;
        this.page.pageSize = dataRes.size;
        this.page.currentPage = dataRes.current;
        this.listData = dataRes.records || [];
        if (this.language != "zh") {
          this.listOption.column.forEach((item) => {
            let width = getTextWidth(item.label).nodeWidth + 100;
            item.width = width;
          });
        }

        this.loading = false;
      });
    },
  },
};
</script>
